package com.example.blenderandroid;

import android.app.Activity;
import android.app.AlertDialog;
import android.bluetooth.BluetoothAdapter;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
	private static final int REQUEST_ENABLE_BT = 1;
	
	private Button m_LastDeviceButton;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    	
        setContentView(R.layout.activity_main);
        
        m_LastDeviceButton = (Button) findViewById(R.id.button_last_device);
    }
    
    @Override
    protected void onStart() {
    	super.onStart();
        
    	// test if the device running this app supports bluetooth
    	BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
//TODO enable the test below when actually deploying
    	if (bluetoothAdapter == null) {
    		showNoBluetoothDialog();
    		return;
    	}
    	
//TODO use the ACTION_STATE_CHANGED broadcast Intent in order to listen for changes to the bluetooth state as the app is running
    	if (!bluetoothAdapter.isEnabled()) {
    	    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    	    startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
    	}
    	
    	m_LastDeviceButton.setEnabled(false);
//TODO load the device last used by this app - if one does not exist, disable the last device button.
//     We also need to save this device in an onStop() call somewhere.
//        m_LastDeviceButton.setEnabled(?);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
    private void showNoBluetoothDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		LayoutInflater inflater = this.getLayoutInflater();
		builder.setView(inflater.inflate(R.layout.dialog_no_bluetooth, null));
		builder.setPositiveButton(R.string.ok_text, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				MainActivity.this.finish();
			}
		});
		AlertDialog dialog = builder.create();
		dialog.show();
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    	switch (requestCode) {
    	case REQUEST_ENABLE_BT:
    		if (resultCode == RESULT_CANCELED) {
    			finish();
    		}
    		break;
    	}
    }
    
	public void onDiscoverClicked(View view) {
		Intent connectIntent = new Intent(this, DiscoverBtDevicesActivity.class);
		startActivity(connectIntent);
	}
    
	public void onConnectClicked(View view) {
		Intent connectIntent = new Intent(this, SelectPairedDeviceActivity.class);
		startActivity(connectIntent);
	}
    
	public void onConnectLastClicked(View view) {
//TODO start connect to last activity
	}
}
